﻿package com.eitb.programas.costa{
	import flash.geom.*;
	import flash.events.*;
	import com.eitb.maps.*;
	import com.eitb.formaters.*;
	import com.eitb.programas.costa.entidades.*;
	import com.google.maps.*;
	import com.google.maps.overlays.*;
	import flash.external.*;

	public class ModeloCostaWindGuru implements IControladorCosta {

		private var vCosta:IVistaCosta;
		private var idioma:int;
		private var map:Map;
		private var xmlData:XML;
		private var mapLatLng:LatLng;
		private var mapZoom:int;

		private var puntos:Array;

		public function ModeloCostaWindGuru():void {
			this.puntos = new Array();
		}
		
		public function initVistaCosta(vCosta:IVistaCosta):void {
			this.vCosta = vCosta;
		}
		
		public function initIdioma(idioma:int):void {
			this.idioma = idioma;
		}
		
		public function initXmlData(xmlData:XML) {
			this.xmlData = xmlData;
		}
		
		public function initMap(rectangle:Rectangle, latLng:LatLng, zoom:int):void {
			this.mapLatLng = latLng;
			this.mapZoom = zoom;
			this.map = new Map();
			this.map.key = Keys.MAP_KEY_WWW_EITB_COM;
			this.map.x = rectangle.x;
			this.map.y = rectangle.y;
			this.map.setSize(new Point(rectangle.width, rectangle.height));
			this.map.addEventListener(MapEvent.MAP_PREINITIALIZE, this.onMapPreinitialize);
			this.map.addEventListener(MapEvent.MAP_READY, this.onMapReady);
			this.vCosta.addChild(this.map);
		}
		
		public function initPunto(punto:Punto):void {
			this.puntos.push(punto);
		}
		
		private function onMapPreinitialize(event:Event):void {
			this.map.setInitOptions(new MapOptions({
			         mapType: MapType.SATELLITE_MAP_TYPE,
			         mapTypes: null,
			         center: this.mapLatLng,
			         zoom: this.mapZoom,   
			   controlByKeyboard:false,
			   crosshairs:false,
			   doubleClickMode: MapAction.ACTION_NOTHING,
			   dragging: false
			   }));
		}
		
		private function onMapReady(event:Event):void {
			for each (var punto:Punto in this.puntos) {
				var m:WindGuruMarker = new WindGuruMarker(punto.getLatLng());
				var mOptions:MarkerOptions = new MarkerOptions({
				     strokeStyle: {
				       color: 0xFFFFFF
				     },
				     fillStyle: {
				       color: 0xFFFFFF,
				       alpha: 0.83
				     },
				     //label: punto.getLabel(),
				     labelFormat: {
				       bold: true
				     },
				     tooltip: punto.getTooltip(),
				     radius: 3,
				     hasShadow: true,
				     clickable: true,
				     draggable: false,
				     gravity: 2,
				     distanceScaling: false
				   });
				m.setOptions(mOptions);
				m.setWindGuru(punto.getWindGuru());
				m.addEventListener(MapMouseEvent.CLICK, this.onMarkerClick);
				this.map.addOverlay(m);
			}
		}
		
		private function onMarkerClick(event:MapMouseEvent):void {
			ExternalInterface.call("windguru_forecast", event.target.getWindGuru().getCodigoPostal(), event.target.getWindGuru().getCodigo(), new WindGuruLanguageFormater(this.idioma).format());
		}
	}
}